﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapBASTGA(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        } 
        public void SaveBASTGA(DataRow dwData, DataRow dwDispo)
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryBASTGA(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapBASTGA(ref dwLocal, dwData);
            InsertBASTGA(dwLocal);
            UpdateBASTGA(dwLocal);
        }
        public DataSet QueryBASTGA(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM BASTGA WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM BASTGA WHERE  "
                    + "  BAGMANDANT=:BAGMANDANT AND BAGART=:BAGART AND BAGVAR=:BAGVAR AND BAGFARBE=:BAGFARBE AND BAGPOS=:BAGPOS";
                    or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = dwData["BAGMANDANT"].ToString();
                    or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = dwData["BAGART"].ToString();
                    or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = dwData["BAGVAR"].ToString();
                    or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = dwData["BAGFARBE"].ToString(); ;
                    or1.Parameters.Add("BAGPOS", OracleType.NVarChar).Value = dwData["BAGPOS"].ToString(); ;
                    break;
                case 2:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT DISTINCT WF.*,GSCBEZ,0 AS ADDLENGTH FROM WDECK WD "
                        + " JOIN WASTKE ON WDMANDANT=WAKMANDANT AND WDART=WAKART AND WDVAR=WAKVAR AND WDFARBE=WAKWCOL"
                        + " JOIN BASTGA WF ON BAGMANDANT=WAKMANDANT AND BAGART=WAKKART AND BAGVAR=WAKKVAR AND BAGFARBE=WAKKFARBE"
                        + " JOIN GSCOLOR ON GSCART=BAGGART AND GSCVAR=BAGGVAR AND  GSCFARBE= BAGGFARBE"
                        + " WHERE WDMANDANT=:WDMANDANT AND WDSA='K' AND WDTYP='B' AND WDWAKZ='F' AND WDAUFKRS=:WDAUFKRS  AND WDAUFLFD=:WDAUFLFD";
                    or1.Parameters.Add("WDMANDANT", OracleType.NVarChar).Value = dwData["WDMANDANT"].ToString();
                    or1.Parameters.Add("WDAUFKRS", OracleType.NVarChar).Value = dwData["WDAUFKRS"].ToString();
                    or1.Parameters.Add("WDAUFLFD", OracleType.NVarChar).Value = dwData["WDAUFLFD"].ToString();
                    break;
                case 3:
                    or1.CommandText = "SELECT * FROM BASTGA WHERE  "
                    + "  BAGMANDANT=:BAGMANDANT AND BAGART=:BAGART AND BAGVAR=:BAGVAR AND BAGFARBE=:BAGFARBE";
                    or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = dwData["BAGMANDANT"].ToString();
                    or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = dwData["BAGART"].ToString();
                    or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = dwData["BAGVAR"].ToString();
                    or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = dwData["BAGFARBE"].ToString(); ;
                    break;
                case 4:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT DISTINCT WF.* FROM WDECK WD RIGHT JOIN BASTGA WF ON"
                        + "  BAGMANDANT=WDMANDANT  AND BAGART=WDART AND BAGVAR='1 '"
                        + " WHERE WDMANDANT=:WDMANDANT AND WDSA='K' AND WDTYP='B' AND WDWAKZ='F' AND WDAUFKRS=:WDAUFKRS  AND WDAUFLFD=:WDAUFLFD";
                    or1.Parameters.Add("WDMANDANT", OracleType.NVarChar).Value = dwData["WDMANDANT"].ToString();
                    or1.Parameters.Add("WDAUFKRS", OracleType.NVarChar).Value = dwData["WDAUFKRS"].ToString();
                    or1.Parameters.Add("WDAUFLFD", OracleType.NVarChar).Value = dwData["WDAUFLFD"].ToString();
                    break;
                case 5:
                    or1.CommandText = "SELECT DISTINCT M.* FROM BASTGA M JOIN ABPOS ON BAGMANDANT=ABPMANDANT AND BAGART=ABPART AND TRIM(BAGVAR)=TRIM(ABPVAR) AND  BAGFARBE=ABPFARBE  "
                 + " WHERE ABPMANDANT=:ABPMANDANT AND ABPAUFKRS=:ABPAUFKRS AND ABPAUFLFD=:ABPAUFLFD";
                    or1.Parameters.Add("ABPMANDANT", OracleType.NVarChar).Value = dwData["ABPMANDANT"].ToString();
                    or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwData["ABPAUFKRS"].ToString();
                    or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwData["ABPAUFLFD"].ToString();
                    break; 
                default:
                    break;
            } 
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "BASTGA";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
        public void InsertBASTGA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO BASTGA(BAGMANDANT, BAGART, BAGVAR, BAGFARBE, BAGPOS, BAGGART, BAGGVAR, BAGGFARBE, BAGFDZ, BAGABF, BAGKGNET, BAGKGBRUT, BAGDISPOKZ, BAGBEM, BAGVARKZ, BAGAUFMSL, BAGFILLER)"
                + "VALUES (:BAGMANDANT, :BAGART, :BAGVAR, :BAGFARBE, :BAGPOS, 'J100                ', '3  ', '          ', 8308, 0, 98126, 98126, 'B', '                    ', ' ', '  ', '                  ')";
            or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = dwData["BAGMANDANT"].ToString();
            or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = dwData["BAGART"].ToString();
            or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = dwData["BAGVAR"].ToString();
            or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = dwData["BAGFARBE"].ToString();
            or1.Parameters.Add("BAGPOS", OracleType.NVarChar).Value = dwData["BAGPOS"].ToString();
            setResult = Connect2Proxy(or1);
        }
        public void DeleteBASTGA(string client, string art, string var, string color)
        {

            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE BASTGA WHERE  "
            + "  BAGMANDANT=:BAGMANDANT AND BAGART=:BAGART AND BAGVAR=:BAGVAR AND BAGFARBE=:BAGFARBE";
            or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = client;
            or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = art;
            or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = var;
            or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = color;
            Connect2Proxy(or1);
        }
        public void UpdateBASTGAD(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE BASTGAD SET "
                + "BAGMANDANT=:BAGMANDANT,"
                + "BAGART=:BAGART,"
                + "BAGVAR=:BAGVAR,"
                + "BAGFARBE=:BAGFARBE,"
                + "BAGPOS=:BAGPOS,"
                + "BAGGART=:BAGGART,"
                + "BAGGVAR=:BAGGVAR,"
                + "BAGGFARBE=:BAGGFARBE,"
                + "BAGFDZ=:BAGFDZ,"
                + "BAGABF=:BAGABF,"
                + "BAGKGNET=:BAGKGNET,"
                + "BAGKGBRUT=:BAGKGBRUT,"
                + "BAGDISPOKZ=:BAGDISPOKZ,"
                + "BAGBEM=:BAGBEM,"
                + "BAGVARKZ=:BAGVARKZ,"
                + "BAGAUFMSL=:BAGAUFMSL,"
                + "BAGFILLER=:BAGFILLER "
                + " WHERE  "
                + "  BAGMANDANT=:BAGMANDANT AND BAGART=:BAGART  AND BAGVAR=:BAGVAR  AND BAGFARBE=:BAGFARBE  AND BAGPOS=:BAGPOS";
            or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = dwData["BAGMANDANT"].ToString();
            or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = dwData["BAGART"].ToString();
            or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = dwData["BAGVAR"].ToString();
            or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = dwData["BAGFARBE"].ToString();
            or1.Parameters.Add("BAGPOS", OracleType.NVarChar).Value = dwData["BAGPOS"].ToString();
            or1.Parameters.Add("BAGGART", OracleType.NVarChar).Value = dwData["BAGGART"].ToString();
            or1.Parameters.Add("BAGGVAR", OracleType.NVarChar).Value = dwData["BAGGVAR"].ToString();
            or1.Parameters.Add("BAGGFARBE", OracleType.NVarChar).Value = dwData["BAGGFARBE"].ToString();
            or1.Parameters.Add("BAGFDZ", OracleType.NVarChar).Value = dwData["BAGFDZ"].ToString();
            or1.Parameters.Add("BAGABF", OracleType.NVarChar).Value = dwData["BAGABF"].ToString();
            or1.Parameters.Add("BAGKGNET", OracleType.NVarChar).Value = dwData["BAGKGNET"].ToString();
            or1.Parameters.Add("BAGKGBRUT", OracleType.NVarChar).Value = dwData["BAGKGBRUT"].ToString();
            or1.Parameters.Add("BAGDISPOKZ", OracleType.NVarChar).Value = dwData["BAGDISPOKZ"].ToString();
            or1.Parameters.Add("BAGBEM", OracleType.NVarChar).Value = dwData["BAGBEM"].ToString();
            or1.Parameters.Add("BAGVARKZ", OracleType.NVarChar).Value = dwData["BAGVARKZ"].ToString();
            or1.Parameters.Add("BAGAUFMSL", OracleType.NVarChar).Value = dwData["BAGAUFMSL"].ToString();
            or1.Parameters.Add("BAGFILLER", OracleType.NVarChar).Value = dwData["BAGFILLER"].ToString();
            Connect2Proxy(or1);
        }
        public void UpdateBASTGA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE BASTGA SET "
                + "BAGMANDANT=:BAGMANDANT,"
                + "BAGART=:BAGART,"
                + "BAGVAR=:BAGVAR,"
                + "BAGFARBE=:BAGFARBE,"
                + "BAGPOS=:BAGPOS,"
                + "BAGGART=:BAGGART,"
                + "BAGGVAR=:BAGGVAR,"
                + "BAGGFARBE=:BAGGFARBE,"
                + "BAGFDZ=:BAGFDZ,"
                + "BAGABF=:BAGABF,"
                + "BAGKGNET=:BAGKGNET,"
                + "BAGKGBRUT=:BAGKGBRUT,"
                + "BAGDISPOKZ=:BAGDISPOKZ,"
                + "BAGBEM=:BAGBEM,"
                + "BAGVARKZ=:BAGVARKZ,"
                + "BAGAUFMSL=:BAGAUFMSL,"
                + "BAGFILLER=:BAGFILLER "
                + " WHERE  "
                + "  BAGMANDANT=:BAGMANDANT AND BAGART=:BAGART  AND BAGVAR=:BAGVAR  AND BAGFARBE=:BAGFARBE  AND BAGPOS=:BAGPOS";
            or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = dwData["BAGMANDANT"].ToString();
            or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = dwData["BAGART"].ToString();
            or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = dwData["BAGVAR"].ToString();
            or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = dwData["BAGFARBE"].ToString();
            or1.Parameters.Add("BAGPOS", OracleType.NVarChar).Value = dwData["BAGPOS"].ToString();
            or1.Parameters.Add("BAGGART", OracleType.NVarChar).Value = dwData["BAGGART"].ToString();
            or1.Parameters.Add("BAGGVAR", OracleType.NVarChar).Value = dwData["BAGGVAR"].ToString();
            or1.Parameters.Add("BAGGFARBE", OracleType.NVarChar).Value = dwData["BAGGFARBE"].ToString();
            or1.Parameters.Add("BAGFDZ", OracleType.NVarChar).Value = dwData["BAGFDZ"].ToString();
            or1.Parameters.Add("BAGABF", OracleType.NVarChar).Value = dwData["BAGABF"].ToString();
            or1.Parameters.Add("BAGKGNET", OracleType.NVarChar).Value = dwData["BAGKGNET"].ToString();
            or1.Parameters.Add("BAGKGBRUT", OracleType.NVarChar).Value = dwData["BAGKGBRUT"].ToString();
            or1.Parameters.Add("BAGDISPOKZ", OracleType.NVarChar).Value = dwData["BAGDISPOKZ"].ToString();
            or1.Parameters.Add("BAGBEM", OracleType.NVarChar).Value = dwData["BAGBEM"].ToString();
            or1.Parameters.Add("BAGVARKZ", OracleType.NVarChar).Value = dwData["BAGVARKZ"].ToString();
            or1.Parameters.Add("BAGAUFMSL", OracleType.NVarChar).Value = dwData["BAGAUFMSL"].ToString();
            or1.Parameters.Add("BAGFILLER", OracleType.NVarChar).Value = dwData["BAGFILLER"].ToString();
            Connect2Proxy(or1);
        }
        
        
    }
}
